<?php

namespace app\admin\model;
use think\Model;
use think\Db;


class RightModel extends Model
{

    const ADMIN_MODULE_RIGHT = 0;
    const GOODS_MODULE_RIGHT = 1;
    const ORDER_MODULE_RIGHT = 2;
    const USER_MODULE_RIGHT = 3;
    const ANNOUNCE_MODULE_RIGHT = 4;
    const COUPON_MODULE_RIGHT = 5;

    

    public static $module_rights = array(
        'admin' => self::ADMIN_MODULE_RIGHT, 'goods' => self::GOODS_MODULE_RIGHT,
        'order' => self::ORDER_MODULE_RIGHT, 'user' => self::USER_MODULE_RIGHT,
        'announce' => self::ANNOUNCE_MODULE_RIGHT, 'coupon' => self::COUPON_MODULE_RIGHT
    );

    public static $module_rights_desc = array(
        'admin' => '管理员模块', 'goods' => '商品模块',
        'order' => '订单模块', 'user' => '用户模块',
        'announce' => '公告模块', 'coupon' => '优惠券模块'
    );

    public static $module_right_action = array(
        'admin' => array(
            '/user'
        ),

        'goods' => array(
            '/goods'
        ),
        'order' => array(
            '/order'
        ),
        'user' => array(
            '/user'
        ),
        'announce' => array(
            '/announce'
        ),
        'coupon' => array(
            '/coupon'
        )

    );
	//不需要权限，可调用的方法名
    public static $common_actions = array(
        
    );
	//获取 请求方法名 需要的 权限 名
    private static function getActionPrivilegeModule()
    {

        $s = __ACTION__; //当请求URL 省略index 方法名时，该常量会添加该 index 方法名

        $s = $s[0] == '/' ? substr($s, 1) : $s;
        $pos = strpos($s, '/');
        $_action = $pos === false ? $s . '/' : substr($s, $pos + 1) . '/';
        
        if (in_array($_action, self::$common_actions)) {
            return null;
        }
        $res = array();
        foreach (self::$module_right_action as $module => $actions) {
            foreach ($actions as $action) {
                $len = strlen($action);
                if (strncmp($_action, $action, $len) == 0) {
                    $res[] = $module;
                }
            }
        }
        return empty($res) ? null : $res;

    }

    public static function checkAdminPrivilege()
    {
        $module = self::getActionPrivilegeModule();
		//var_dump($module);
        if (is_null($module)) {
            return true;
        } else if ($module) {
            $admin_right = session('admin_right');

            foreach ($module as $val) {
                if (self::isRightInfo(self::$module_rights[$val], $admin_right)) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }


    }
	//判断是否有指定模块的权限
    public static function isRightInfo($right_id, $admin_right)
    {
        $mask = 1 << $right_id;
        return ($mask & $admin_right) == $mask;

    }
	//生成新权限值
    public static function calcNewRight($right_id, $old_info)
    {
        $mask = 1 << $right_id;
        return $old_info | $mask;

    }
    public static function generateRightByModuleName($module_arr = array())
    {
        $right = 0;
        if (empty($module_arr)) {
            return $right;
        }

        foreach ($module_arr as $val) {
            if (key_exists($val, self::$module_rights)) {
                $right = self::calcNewRight(self::$module_rights[$val], $right);
            }
        }
        return $right;
    }
    public static function getRightName($right)
    {
        $res = array();
        if (!$right) {
            return $res;
        }
        foreach (self::$module_rights as $key => $val) {
            if (self::isRightInfo($val, $right)) {
                $res[] = $key;
            }
        }
        return $res;
    }
}
?>